import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:sports_new/entity/other/resource_entity.dart';
import 'package:sports_new/server/api/other_api.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/live/items/live_message_list.dart';

import '../../../utils/utils.dart';

class LiveAds extends StatefulWidget {
  const LiveAds({super.key, required this.scene});

  final MessageScene scene;

  @override
  State<LiveAds> createState() => _LiveAdsState();
}

class _LiveAdsState extends State<LiveAds> {
  List<ResourceEntity>? liveAds;
  List<bool>? show;

  Future getLiveAds() async {
    List<ResourceEntity>? data = await OtherApi.getAppList("room_chat");
    liveAds = data?.where((element) => element.button == "ads").toList();
    show = liveAds?.map((e) => true).toList();
    setState(() {});
  }

  @override
  void initState() {
    getLiveAds();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return liveAds.noValue?Container():Column(
      mainAxisSize: MainAxisSize.min,
      children: List<Widget>.generate(
        liveAds?.length??0,
        (index) => !(show?[index]??true)?Container():Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Image.asset(
                width: 16,
                Utils.getImgPath("search_close.png")
            ).tap(() => setState(() => show?[index] = false)),
            GestureDetector(
              onTap: (){
                Utils.onEvent(
                    widget.scene == MessageScene.normal?'spzb':'hpzb',
                    params: {
                      (widget.scene == MessageScene.normal?'spzb':'hpzb'):
                      'ads_$index'
                    }
                );
                if(liveAds![index].href != null) {
                  Utils.doJump(liveAds![index].href!);
                }
              },
              child: CachedNetworkImage(
                width: 60,height: 60,
                fit: BoxFit.cover,
                imageUrl: liveAds![index].imgUrl ?? "",
                errorWidget: (context,url,error) => Container(),
              ),
            ),
          ]
        )
      ).insertEvery(Container(height: 4))
    );
  }
}
